package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;
    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏:geofenceQuery={}",geofenceQuery);
        List<GeofenceVO> list=geofenceMapper.selectGeofence(geofenceQuery);
        /* 遍历每个围栏,为其补全车辆数据 */
        for(int i = 0; i < list.size(); i++){
            GeofenceVO geofenceVO = list.get(i);
            //封装车辆的查询条件
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());//查指定围栏上绑定的车
            //注入车辆持久层,调用已有方法查车
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
                //获取当前围栏上绑定的车辆总数
                int totalNum = vehicleVOList.size();
                //定义变量用来保存可用车辆数据
                int availableNum = 0;
                //遍历车辆列表,如果当前车辆状态为空闲,可用车辆数+1
                for(VehicleVO vehicleVO : vehicleVOList){
                    if(vehicleVO.getStatus().equals("1")){
                        availableNum++;
                    }
                }
                //给当前GeofenceVO补全数据
                geofenceVO.setAvailableNum(availableNum);
                geofenceVO.setTotalNum(totalNum);
                geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceParam) {
        log.debug("新增电子围栏:geofenceParam={}",geofenceParam);
        Geofence geofence=new Geofence();
        BeanUtils.copyProperties(geofenceParam,geofence);
        geofence.setStatus("1");
        geofence.setCreateTime(new Date());
        geofenceMapper.insert(geofence);
        log.debug("新增电子围栏成功");
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改电子围栏状态:id={},status={}",id,status);
        Geofence geofence=new Geofence();
        geofence.setId(id);
        geofence.setStatus(status);
        geofence.setUpdateTime(new Date());
        geofenceMapper.update(geofence);
        log.debug("修改电子围栏状态成功");
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏:id={}",id);
        /*删除要思考是否有关联数据，有关联时提示先移除*/
        /*geofenceMapper.deleteById(id);*/
        //1，封装车辆的查询条件
        VehicleQuery vehicleQuery=new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.根据条件查询数据
        List<VehicleVO> list=vehicleMapper.selectVehicle(vehicleQuery);
        if(list != null && list.size() > 0){
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        }else{
            geofenceMapper.deleteById(id);
        }
        log.debug("删除电子围栏成功");
    }

    @Override
    public void unbindVehicle(Long vehicleId) {
        log.debug("解绑电子围栏:vehicleId={}",vehicleId);
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.updateNullValue(vehicle);
        log.debug("解绑电子围栏成功");
    }


}
